package util;


import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.GlobalCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.config.v1.region.ConfigRegion;
import com.huaweicloud.sdk.config.v1.*;
import com.huaweicloud.sdk.config.v1.model.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.PropertyResourceBundle;

/**
 * 查看子账号的资源详情
 *
 */
public class ShowAggregateResourceConfig {

    public static String ak="";
    public static String sk="";
    public static void main(String[] args) {
        //读取resources内properties配置文件中的秘钥信息, 该段根据自己需要, 写死也可以 add by sijiacheng 20230811 start
        try {
            InputStream inputStream = ClassLoader.getSystemResourceAsStream("huaweicloud.properties");
            PropertyResourceBundle bundle = new PropertyResourceBundle(inputStream);
            ak=bundle.getString("ak");
            sk=bundle.getString("sk");
        } catch (IOException e) {
            System.out.println("从resources内properties配置文件中,获取密钥失败。");
            throw new RuntimeException(e);
        }
        /*// Endpoint请按实际情况填写。
        String endPoint = "https://obs.cn-east-3.myhuaweicloud.com";
        String ak = "UEYW*******KSV";
        String sk = "PnXLi************9su5IkhDRB";*/
        //读取resources内properties配置文件中的秘钥信息, 该段根据自己需要, 写死也可以 add by sijiacheng 20230811 end

        ICredential auth = new GlobalCredentials()
                .withAk(ak)
                .withSk(sk);

        ConfigClient client = ConfigClient.newBuilder()
                .withCredential(auth)
                .withRegion(ConfigRegion.valueOf("cn-north-4"))
                .build();
        ShowAggregateResourceConfigRequest request = new ShowAggregateResourceConfigRequest();
        AggregateResourceConfigRequest body = new AggregateResourceConfigRequest();
        ResourceIdentifier resourceIdentifierbody = new ResourceIdentifier();
        //子账号的实例信息
        resourceIdentifierbody.withResourceId("1ec325fb-e899-42f9-b472-a215cb9057fa")
                .withProvider("ecs")
                .withType("cloudservers")
                .withSourceAccountId("702c174259904017a6efde8aabfe2030")
                .withRegionId("cn-east-3");
        body.withResourceIdentifier(resourceIdentifierbody);
        //主账号的资源聚合器ID
        body.withAggregatorId("57426ec3dd984989907b7f738f831a23");
        request.withBody(body);
        try {
            ShowAggregateResourceConfigResponse response = client.showAggregateResourceConfig(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}
